package com.runen.wnhz.runen.ui.activity.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.Base64Utils;
import com.runen.wnhz.runen.common.utils.Constants;
import com.runen.wnhz.runen.common.utils.JumpUtlis;
import com.runen.wnhz.runen.common.utils.NetUtils;
import com.runen.wnhz.runen.common.utils.OkHttpUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.BaseEntity;
import com.runen.wnhz.runen.data.entity.MineBean;
import com.runen.wnhz.runen.data.entity.UerLogBean;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.ui.activity.main.MainActivity;
import com.runen.wnhz.runen.widget.TitleBuilder;
import com.yzq.zxinglibrary.common.Constant;

import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018-04-10.
 */

public class LoginSetPasdActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_input_pasd)
    EditText etInputPasd;
    @BindView(R.id.et_input_repasd)
    EditText etInputRepasd;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    @Override
    protected int getLayoutView() {
        return R.layout.login_set_password_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {

    }

    @Override
    protected void initStart() {

    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {

    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {

    }

    public void SureMesag(String phone, String pasd,String citycode,String companyCode,String jpushid,String user_type,String type,String openid,String imgurl,String nickname) {

        FormBody formBody;
        String base64Mobile = Base64Utils.encode(phone);
        String base64PassWord = Base64Utils.encode(pasd);
        String jpush_registid = JPushInterface.getRegistrationID(getApplicationContext());
        if(user_type.equals("2")){
             formBody = new FormBody.Builder().add("mobile", base64Mobile).add("password", base64PassWord)
                    .add("city_code", citycode).add("company_code", companyCode)
                    .add("jpush", jpush_registid).add("user_type", user_type)
                    .add("type", type).add("openid", openid)
                    .add("imgurl", imgurl).add("nickname", nickname)
                    .build();
        }else{
             formBody = new FormBody.Builder().add("mobile", base64Mobile).add("password", base64PassWord)
                    .add("jpush", jpush_registid).add("user_type", user_type)
                    .add("type", type).add("openid", openid)
                    .add("imgurl", imgurl).add("nickname", nickname)
                    .build();
        }

        Request.Builder builder = new Request.Builder().url(NetUtils.User_resBythreeRarty).post(formBody);
        Call newCall = OkHttpUtils.getOkHttpClient().newCall(builder.build());
        newCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.showToast("数据请求失败");
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            handleUserCollectionData(response);
                        } catch (IOException e) {
                            e.printStackTrace();
                            ToastUtil.showToast("数据请求异常");
                        }
                    }
                });
            }
        });

    }

    private void handleUserCollectionData(Response response) throws IOException {
        if (response.isSuccessful()) {
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                String json = responseBody.string();
                UerLogBean mybean = new Gson().fromJson(json, UerLogBean.class);
                if (mybean.getRe().equals("1")) {

                    if (mybean.getData() != null){
                        String token =  mybean.getData().toString();
                        String username = mybean.getData().getUsername();
                        String headimg = mybean.getData().getHead_img();
                        String phone = Constants.mobile;
                        ACacheUtlis.getInstance().IsFirstToApp(this, Constants.ISFIRSTOAPP);
                        ACacheUtlis.getInstance().isLogin(this, "true");
                        ACacheUtlis.getInstance().IsRequstUser(this, token, username,headimg, phone);

                        String rid = JPushInterface.getRegistrationID(getApplicationContext());
                        JPushInterface.setAlias(this,Constants.JPUSH_SEQUEENS,rid);
//            connectRongIM(data);
                        JumpUtlis.getInstance().jumpActivity(this,MainActivity.class);
                        finish();

                    }else{
                        ToastUtil.showToast(mybean.getInfo());
                    }
//                    JumpUtlis.getInstance().jumpActivity(this, VerificationActivity.class);
                } else if (mybean.getRe().equals("0")) {
                    ToastUtil.showToast(mybean.getInfo());
                }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_login:
                if(!TextUtils.isEmpty(etInputPasd.getText()) && !TextUtils.isEmpty(etInputRepasd.getText()) && etInputPasd.getText().toString().equals(etInputRepasd.getText().toString())){
                    String phone = Constants.mobile;
                    String pasd = etInputPasd.getText().toString().trim();
                    String joleType =  Constants.jole_type;

                    String cityCode = "";
                    String companyCode = "";
                    if(joleType.equals("2")){
                         cityCode = Constants.cityCode;
                         companyCode = Constants.companyCode;
                    }

                    String type = ACacheUtlis.getInstance().getWxInfo(this).getLogtype();
                    String openid = ACacheUtlis.getInstance().getWxInfo(this).getOpenid();
                    String imgUrl = ACacheUtlis.getInstance().getWxInfo(this).getHeadimg();
                    String nickname = ACacheUtlis.getInstance().getWxInfo(this).getNickname();

                    SureMesag(phone,pasd, cityCode,companyCode,"",joleType,type,openid,imgUrl,nickname);
                }else{
                   ToastUtil.showToast("密码不得为空");
                }
                break;
        }
    }
}
